import LevelObjBase from "./LevelObjBase";
import LevelBaseControl from "./LevelBaseControl";
import WallControl from "./WallControl";
import CameraControl from "../Camera/CameraControl";
import { utils } from "../../utils/CommonUtil";

export default class RoomControl extends LevelObjBase {

    /** 墙*/ 
    private wallControls:Array<WallControl> = new Array<WallControl>();

    private mowner :Laya.Sprite3D;
    /** 相机*/
    private  cameraControl:CameraControl;
    private get CameraControl():CameraControl
    {
            if (this.cameraControl == null)
            {
                this.cameraControl = window.gameFacade.LevelMng.GetCurLevelCamera();
            }
            return this.cameraControl;
    }

    onAwake() {
        this.cameraMaxZoomDis = 10;
        this.mowner = this.owner as Laya.Sprite3D;
        super.onAwake();
        this.InitComponent();
    }

    onStart()
    {
        /** 一开始 直接是刷新墙的位置状态*/
        this.RefreshWall();
    }
    private InitComponent():void
    {
        let wallRoot:Laya.Sprite3D = this.owner.getChildByName("walls") as Laya.Sprite3D;        
        for (let i = 0; i < 4; i++)
        {
            this.wallControls.push(utils.CommonUtils.CustomAddComponent(wallRoot.getChildAt(i) as Laya.Sprite3D,WallControl))
        }       
    }
    /** 刷新墙的位置  直接改变*/
    public RefreshWall():void
    {
        /** 当前相机的Y旋转*/
        let curYRotate:number = Math.floor(this.CameraControl.curRoateY);
        curYRotate = curYRotate % 360;
        /** 取正*/
        if (curYRotate < 0)
        {
            curYRotate += 360;
        }

        /** 根据角度 进行墙的移动*/
        switch (curYRotate)
        {
            case 0:
                this.wallControls[0].SetDownPos();
                this.wallControls[1].SetDownPos();
                this.wallControls[2].SetUpPos();
                this.wallControls[3].SetUpPos();
                break;
            case 90:
                this.wallControls[0].SetDownPos();
                this.wallControls[1].SetUpPos();
                this.wallControls[2].SetUpPos();
                this.wallControls[3].SetDownPos();
                break;
            case 180:
                this.wallControls[0].SetUpPos();
                this.wallControls[1].SetUpPos();
                this.wallControls[2].SetDownPos();
                this.wallControls[3].SetDownPos();
                break;
            case 270:
                this.wallControls[0].SetUpPos();
                this.wallControls[1].SetDownPos();
                this.wallControls[2].SetDownPos();
                this.wallControls[3].SetUpPos();
                break;
            default:
                break;
        }
    }
    /** 根据相机旋转改变墙的位置 移动改变*/
    public  ChangeWall():void
    {
        /** 当前相机的Y旋转*/
        let curYRotate:number =  Math.floor(this.CameraControl.curRoateY);
        curYRotate = curYRotate % 360;
        /** 取正*/
        if (curYRotate < 0)
        {
            curYRotate += 360;
        }

        /** 根据角度 进行墙的移动*/
        switch (curYRotate)
        {
            case 0:
                this.wallControls[0].MoveDown();
                this.wallControls[1].MoveDown();
                this.wallControls[2].MoveUp();
                this.wallControls[3].MoveUp();
                break;
            case 90:
                this.wallControls[0].MoveDown();
                this.wallControls[1].MoveUp();
                this.wallControls[2].MoveUp();
                this.wallControls[3].MoveDown();
                break;
            case 180:
                this.wallControls[0].MoveUp();
                this.wallControls[1].MoveUp();
                this.wallControls[2].MoveDown();
                this.wallControls[3].MoveDown();
                break;
            case 270:
                this.wallControls[0].MoveUp();
                this.wallControls[1].MoveDown();
                this.wallControls[2].MoveDown();
                this.wallControls[3].MoveUp();
                break;
            default:
                break;
        }
    }   
}